/**
 * @file ThemeSwitch
 * @description 主题切换组件
 * @date 2021-05-10 16:30:00
 * @lastModify wenfh2021-05-10 16:30:00
 * 
 */

import React,{useEffect} from 'react'
import { Switch,theme } from 'antd';
import { useAntdConfig, useAntdConfigSetter } from  '@umijs/max';
import { setTheme } from '@/utils/index'
const { darkAlgorithm, defaultAlgorithm } = theme;
const ThemeSwitch: React.FC = () => {
    const setAntdConfig = useAntdConfigSetter();
    const antdConfig = useAntdConfig();
    useEffect(()=>{
        setTheme(antdConfig?.theme?.algorithm?.includes(darkAlgorithm)?'drak':'algor')
    },[()=>antdConfig?.theme?.algorithm])

    return (
        <Switch 
        checked={antdConfig?.theme?.algorithm?.includes(darkAlgorithm)}
        onChange={(data:boolean)=>{
            console.log(data)
            // 此配置会与原配置深合并
            setAntdConfig({
                            theme: {
                                algorithm: [
                                    data ? darkAlgorithm : defaultAlgorithm,
                                ],
                            }
                          })
            }}></Switch>
    )
}

export default ThemeSwitch